﻿using System;
using System.Windows;
using System.Windows.Forms;
using DuSQL.API;
namespace DuSQL
{
    /// <summary>
    /// Connect_Add.xaml 的交互逻辑
    /// </summary>
    public partial class Connect_Add : Window
    {
        public string ConnectID = "";
        public Connect_Add()
        {
            InitializeComponent();
        }
        public Connect_Add(string connectID)
        {
            InitializeComponent();
            ConnectID = connectID;
        }
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }

        private void btnBrowser_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog diag = new OpenFileDialog();
            diag.Filter = "数据库文件(*.mdb;*.accdb)|*.mdb;*.accdb";
            diag.ShowDialog();
            txtFilePath.Text = diag.FileName;          
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string filepath = txtFilePath.Text.Trim();
            if (filepath == "")
                return;          

            if (ConnectID != "")
            {
                DbConnect curConn = Comm.MyConnections.Find(i => i.ConnectID == ConnectID);
                if (curConn != null)
                {
                    Comm.MyConnections.Find(i => i.ConnectID == ConnectID).ConnectStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=True", filepath);
                }
            }
            else
            { 
                DbConnect conn = new DbConnect();
                conn.ConnectID = Guid.NewGuid().ToString();
                conn.ConnectName = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                conn.ConnectStr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=True", filepath);
                conn.DbType = NodeType.DbConnection_Access;
                if (Comm.MyConnections.Exists(i => i.ConnectStr == conn.ConnectStr))
                {
                    System.Windows.MessageBox.Show("该数据连接已经存在列表中!");                    
                    return;
                }
                Comm.MyConnections.Add(conn);
            }
            Comm.OnChangeData(sender,e);
            this.Hide();
        }

        
    }
}
